package com.atguigu.cmsService.service.impl;

import com.atguigu.cmsService.entity.CrmBanner;
import com.atguigu.cmsService.mapper.CrmBannerMapper;
import com.atguigu.cmsService.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-08-10
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Resource
    private CrmBannerMapper crmBannerMapper;

    //分页查询轮播图
    @Override
    public IPage<CrmBanner> pageBanner(Long current, Long size) {
        IPage<CrmBanner> page = new Page<>(current,size);
        return crmBannerMapper.selectPage(page,null);
    }

    //添加轮播图
    @Override
    public void addBanner(CrmBanner crmBanner) {
        crmBannerMapper.insert(crmBanner);
    }

    //修改轮播图
    @Override
    public void updateBanner(CrmBanner crmBanner) {
        crmBannerMapper.updateById(crmBanner);
    }

    //删除轮播图
    @Override
    public void deleteBanner(String id) {
        crmBannerMapper.deleteById(id);
    }

    //查询所有轮播图
    @Cacheable(value = "banner",key = "'getBannerList'")
    @Override
    public List<CrmBanner> getBannerList() {
        QueryWrapper<CrmBanner> crmBannerQueryWrapper = new QueryWrapper<>();
        crmBannerQueryWrapper.orderByDesc("id");
        crmBannerQueryWrapper.last("limit 2");
        return crmBannerMapper.selectList(crmBannerQueryWrapper);
    }
}
